import 'package:flutter_getx/app/data/db/entity/group.dart';
import 'package:flutter_getx/app/data/db/entity/group_member.dart';
import 'package:flutter_getx/app/data/db/entity/message.dart';
import 'package:flutter_getx/app/data/db/entity/user.dart';
import 'package:flutter_getx/app/data/db/entity/user_friend.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart' as dio;
import 'package:image_picker/image_picker.dart';
import '../../service/dio_service.dart';
import '../../util/dio/app_response.dart';

class Api {
  final DioService dioService = Get.find();

  Future<List<MessageEntity>> getMessage(uid) async {
    AppResponse appResponse = await dioService.get(
      "/user/message/all",
      queryParameters: {"uid": uid},
    );

    if (appResponse.ok) {
      List list = appResponse.data;
      List<MessageEntity> ans = [];
      list.forEach((message) {
        ans.add(MessageEntity.fromJson(message));
      });
      return ans;
    } else {
      return [];
    }
  }

  Future<List<UserFriendEntity>> getUserFriendEntity(uid) async {
    AppResponse appResponse = await dioService.get(
      "/user/friend/all",
      queryParameters: {"uid": uid},
    );
    if (appResponse.ok) {
      List list = appResponse.data;
      List<UserFriendEntity> ans = [];
      list.forEach((message) {
        ans.add(UserFriendEntity.fromJson(message));
      });
      return ans;
    } else {
      return [];
    }
  }

  Future<List<UserEntity>> getLinkedUsers(id) async {
    AppResponse appResponse = await dioService.get(
      "/user/friend/info",
      queryParameters: {"uid": id},
    );
    if (appResponse.ok) {
      if (appResponse.data != null) {
        return UserEntity.fromJsonList(appResponse.data);
      }
    }
    return [];
  }

  Future<bool> addFriend(int uid, int fid) async {
    AppResponse appResponse = await dioService.post(
      "/user/friend",
      data: {"user_id": uid, "friend_id": fid},
    );
    if (appResponse.ok) {
      return true;
    }
    return false;
  }

  Future<bool> joinFriend(int uid, int groupId) async {
    AppResponse appResponse = await dioService.post(
      "/user/group/join",
      data: {"uid": uid, "group_id": groupId},
    );
    if (appResponse.ok) {
      return true;
    }
    return false;
  }

  Future<List<GroupEntity>> getAllGroup(uid) async {
    AppResponse appResponse =
        await dioService.get("/group", queryParameters: {"uid": uid});
    if (appResponse.ok) {
      if (appResponse.data != null) {
        return GroupEntity.fromJsonList(appResponse.data);
      }
    }
    return [];
  }

  Future<List<GroupMemberEntity>> getAllGroupMember(uid) async {
    AppResponse appResponse =
        await dioService.get("/groupMember", queryParameters: {"uid": uid});
    if (appResponse.ok) {
      if (appResponse.data != null) {
        return GroupMemberEntity.fromJsonList(appResponse.data);
      }
    }
    return [];
  }

  Future<bool> updateUserInfo(int id, bool gender, String nickname,
      String birthday, String intro) async {
    AppResponse appResponse = await dioService.post("/user", data: {
      'id': id,
      'gender': gender,
      'nickname': nickname,
      'birthday': birthday,
      'intro': intro
    });
    return appResponse.ok;
  }

  Future<bool> signUp(String phone, String password, String nickname) async {
    AppResponse appResponse = await dioService.post("/user/sign",
        data: {'nickname': nickname, 'password': password, 'phone': phone});
    return appResponse.ok;
  }

  Future<List> getChatList(uid) async {
    AppResponse appResponse =
        await dioService.get("/user/chat/list", queryParameters: {"id": uid});
    if (appResponse.ok) {
      if (appResponse.data != null) {
        return appResponse.data;
      }
    }
    return [];
  }

  Future updateAvatar(int uid, PickedFile image, String fileName) async {
    dio.FormData formData = dio.FormData.fromMap({
      "uid": uid,
      "file": new dio.MultipartFile.fromBytes(await image.readAsBytes(),
          filename: fileName)
    });

    AppResponse appResponse = await dioService.post(
      "/upload/avatar",
      data: formData,
    );
    if (appResponse.ok) {
      return appResponse.data['url'];
    }
  }

  createGroup(int uid, String name) async {
    AppResponse appResponse = await dioService.post("/group",
        data: {'userId': uid, 'name': name});
    return appResponse.ok;
  }

  Future getGroupInfo(int groupId) async {
    AppResponse appResponse =
    await dioService.get("/group/info", queryParameters: {"id": groupId});
    if (appResponse.ok) {
      return appResponse.data;
    }
    return null;
  }

  Future updateGroupName(int groupId,String groupName) async {
    AppResponse appResponse =
    await dioService.post("/group/name", data: {"group_id": groupId, "group_name": groupName});
    return appResponse.ok;
  }

  Future updateGroupNotice(int groupId,String groupNotice) async {
    AppResponse appResponse =
    await dioService.post("/group/notice", data: {"group_id": groupId, "group_notice": groupNotice});
    return appResponse.ok;
  }
}
